#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2019/12/3 09:48|09:48
# @Author  : yangdingyi
# @File    : 两两交换链表中的节点
# @Software: PyCharm


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        # if not head:
        #     return head
        # p, q = head, head.next
        # while p and q:
        #     p.val, q.val = q.val, p.val
        #     p = q.next
        #     if p:
        #         q = p.next
        # return head
        def work(p: ListNode) -> None:
            if not p:
                return
            q = p.next
            if not q:
                return
            work(q.next)
            p.val, q.val = q.val, p.val
            return

        work(head)
        return head
